/**
 * 作者: wgx
 * 日期: 2025-05-30 10:53:38
 * 备注: 用户大世界事件坑位队伍派遣数据
 */
package com.motu.monstercity.server.game.userdata;

import com.motu.monstercity.protocol.Common.PBUserWorldEventLocationTeam;
import com.motu.vertx.module.utility.model.BaseModel;
import com.motu.vertx.module.utility.model.Model;
import io.vertx.core.json.JsonObject;
import io.vertx.core.json.JsonArray;

public class UserWorldEventLocationTeamModel extends BaseModel implements Model {
    public static final String LISTKEY = "unique_id.team_id";
    public static final int REDIS_EXPIRE_TIME = 0;// redis过期时间
    public static final int UPDATE_DB_TYPE = UPDATE_DB_TYPE_QUEUE;// 数据入库类型 队列入库
    public static final boolean ORDER_LIST = false;// 是否需要额外的有序列表

    protected long id; // id
    protected String uniqueId = ""; // userid+locationid
    protected int locationId; // 坑位id，从1-8，最多8个坑位
    protected int teamId; // 队伍id
    protected long startTime; // 事件的刷新时间
    protected long eventQueueDbid; // 事件的唯一id (用来判断行军的事件是否有效)

    public void reset() {
    }

    protected String getKey() {
        return "UserWorldEventLocationTeam." + uniqueId + "." + teamId;
    }

    protected String getListKey() {
        return "UserWorldEventLocationTeam." + uniqueId;
    }

    protected String getListItemKey() {
        return teamId + "";
    }

    public void doCreate() {
        setTableId();
        insert();
    }

    public void setTableId() {
        putId(incrTableId("user_world_event_location_team"));
    }

    public JsonObject insert() {
        return super.insert(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    public void delete() {
        super.delete(getKey(), id, UPDATE_DB_TYPE, 0, LISTKEY, getListKey(), getListItemKey(), ORDER_LIST);
    }

    /**
     * 根据入库类型更新
     */
    public JsonObject update() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    /**
     * 实时入队列
     */
    public JsonObject updateQueueNow() {
        return super.update(getKey(), id, REDIS_EXPIRE_TIME, UPDATE_DB_TYPE, 0, getAllObj(), LISTKEY, getListKey(), getListItemKey());
    }

    public JsonObject getAllObj(){
        JsonObject obj = new JsonObject();
        obj.put("id", id); // id
        obj.put("uniqueId", uniqueId); // userid+locationid
        obj.put("locationId", locationId); // 坑位id，从1-8，最多8个坑位
        obj.put("teamId", teamId); // 队伍id
        obj.put("startTime", startTime); // 事件的刷新时间
        obj.put("eventQueueDbid", eventQueueDbid); // 事件的唯一id (用来判断行军的事件是否有效)
        return obj;
    }

    public PBUserWorldEventLocationTeam.Builder takeInitBuilder() {
        PBUserWorldEventLocationTeam.Builder sb = PBUserWorldEventLocationTeam.newBuilder();
        if (locationId != 0) {
            sb.setLocationId(locationId);
        }
        if (teamId != 0) {
            sb.setTeamId(teamId);
        }
        return sb;
    }

    public PBUserWorldEventLocationTeam.Builder takeBuilder() {
        if (updateObj == null || updateObj.isEmpty()) {
            return takeInitBuilder();
        }
        PBUserWorldEventLocationTeam.Builder sb = PBUserWorldEventLocationTeam.newBuilder();
        if (isFieldUpdate("locationId")) {
            sb.setLocationId(locationId);
        }
        if (isFieldUpdate("teamId")) {
            sb.setTeamId(teamId);
        }
        return sb;
    }

    public long getId() {
        return id;
    }

    public void putId(long id) {
        if (this.id != id) {
            this.id = id;
            updateRedisHashField(getKey(), "id", this.id);
        }
    }

    public void addId(long value) {
        this.id += value;
        updateRedisHashField(getKey(), "id", this.id);
    }

    public String getUniqueId() {
        return uniqueId;
    }

    public void putUniqueId(String uniqueId) {
        if (!this.uniqueId.equals(uniqueId)) {
            this.uniqueId = uniqueId;
            updateRedisHashField(getKey(), "uniqueId", this.uniqueId);
        }
    }

    public int getLocationId() {
        return locationId;
    }

    public void putLocationId(int locationId) {
        if (this.locationId != locationId) {
            this.locationId = locationId;
            updateRedisHashField(getKey(), "locationId", this.locationId);
        }
    }

    public void addLocationId(int value) {
        this.locationId += value;
        updateRedisHashField(getKey(), "locationId", this.locationId);
    }

    public int getTeamId() {
        return teamId;
    }

    public void putTeamId(int teamId) {
        if (this.teamId != teamId) {
            this.teamId = teamId;
            updateRedisHashField(getKey(), "teamId", this.teamId);
        }
    }

    public void addTeamId(int value) {
        this.teamId += value;
        updateRedisHashField(getKey(), "teamId", this.teamId);
    }

    public long getStartTime() {
        return startTime;
    }

    public void putStartTime(long startTime) {
        if (this.startTime != startTime) {
            this.startTime = startTime;
            updateRedisHashField(getKey(), "startTime", this.startTime);
        }
    }

    public void addStartTime(long value) {
        this.startTime += value;
        updateRedisHashField(getKey(), "startTime", this.startTime);
    }

    public long getEventQueueDbid() {
        return eventQueueDbid;
    }

    public void putEventQueueDbid(long eventQueueDbid) {
        if (this.eventQueueDbid != eventQueueDbid) {
            this.eventQueueDbid = eventQueueDbid;
            updateRedisHashField(getKey(), "eventQueueDbid", this.eventQueueDbid);
        }
    }

    public void addEventQueueDbid(long value) {
        this.eventQueueDbid += value;
        updateRedisHashField(getKey(), "eventQueueDbid", this.eventQueueDbid);
    }

}